from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from app.window.msg_box import Msg_box
from app.window.create_script_group_dialog import Create_script_group_dialog
from database.db import Database
from ui.Ui_script_editor_dialog import Ui_Dialog_script_editor


class Public_script_new_signal(QObject):
    save = Signal(int)


class Public_script_new_dialog(QDialog):
    
    
    def __init__(self, parent=None):
        super(Public_script_new_dialog, self).__init__(parent)
        self.ui = Ui_Dialog_script_editor()
        self.ui.setupUi(self)
        self.setWindowTitle('新建公有脚本')
        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Public_script_new_signal()

        self.ui.comboBox_group.activated.connect(self.slot_comboBox_active)
        
        self.create_group_name_dialog = Create_script_group_dialog()
        self.create_group_name_dialog.signal.save.connect(self.slot_update_group)

        self.ui.pushButton_save.clicked.connect(self.slot_save)
    

    def reload_group(self):
        # 重载脚本分组
        self.ui.comboBox_group.clear()
        db = Database()
        group_list = db.select('script_group',['name'])
        for data in group_list:
            self.ui.comboBox_group.addItem(data[0])
        self.ui.comboBox_group.addItem('<创建分组>')
        db.close_conn()

    def slot_comboBox_active(self,index:int):
        if self.ui.comboBox_group.currentText() == '<创建分组>':
            self.create_group_name_dialog.slot_show()


    def slot_update_group(self,name:str):
        self.reload_group()
        self.ui.comboBox_group.setCurrentText(name)
    

    def slot_save(self):
        group_name = self.ui.comboBox_group.currentText()
        if group_name == '<创建分组>':
            QMessageBox().information(self,'错误!','请选择正确的分组',QMessageBox.Ok)
            return
        
        data = {
            'name':self.ui.lineEdit_name.text(),
            'content':self.ui.textEdit_script.toPlainText(),
            'textfsm':self.ui.textEdit_textfsm.toPlainText(),
            'info':self.ui.textEdit_info.toPlainText(),
            'type':'public',
            'group_name':group_name,

        }
        # 写入数据库
        db = Database()
        fields = ['name','content','textfsm','info','type','group_name']
        values = (data['name'],data['content'],data['textfsm'],data['info'],
                  data['type'],data['group_name'],)
        db.insert('scripts',fields,values)
        script_id = db.select('sqlite_sequence',['seq'],'name="scripts"')[0][0]
        
        db.close_conn()

        # 公有脚本管理器更新ui数据
        self.signal.save.emit(script_id)
        self.hide()

    
    def slot_show(self):
        

        self.ui.lineEdit_name.clear()
        self.ui.textEdit_script.clear()
        self.ui.textEdit_textfsm.clear()
        self.ui.textEdit_info.clear()

        # 重载脚本分组
        self.reload_group()
        self.show()